package com.wind.taobao.api.trade.shipping.convert
{
	import com.wind.taobao.api.trade.shipping.model.Shipping;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Shipping转换器
	 * @author fxy
	 *
	 */
	public class ShippingConvert
	{
		/**
		 * 转换Shipping集合
		 */
		public static function convertShippings(value:Object, type:String="json"):ArrayCollection
		{
			var shippings:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							shippings.addItem(convertShipping((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return shippings;
		}

		/**
		 * 转换单个Shipping对象
		 * value:XML 或 JSON
		 */
		public static function convertShipping(value:Object, type:String="json", shipping:Shipping=null):Shipping
		{
			if (shipping == null)
				shipping=new Shipping();
			if (value == null)
				return shipping;
			if (type == TaobaoConstants.XML)
			{
				shipping=convertXMLToShipping(XML(value), shipping);
			}
			else
			{
				shipping=convertJSONToShipping(value, shipping);
			}
			return shipping;
		}

		/**
		 * JSON转换为Shipping
		 */
		private static function convertJSONToShipping(json:Object, shipping:Shipping):Shipping
		{
			if (json.hasOwnProperty(TaobaoConstants.TID))
			{
				shipping.tid=json[TaobaoConstants.TID];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_NICK))
			{
				shipping.seller_nick=json[TaobaoConstants.SELLER_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_NICK))
			{
				shipping.buyer_nick=json[TaobaoConstants.BUYER_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.DELIVERY_START))
			{
				shipping.delivery_start=json[TaobaoConstants.DELIVERY_START];
			}
			if (json.hasOwnProperty(TaobaoConstants.DELIVERY_END))
			{
				shipping.delivery_end=json[TaobaoConstants.DELIVERY_END];
			}
			if (json.hasOwnProperty(TaobaoConstants.OUT_SID))
			{
				shipping.out_sid=json[TaobaoConstants.OUT_SID];
			}
			if (json.hasOwnProperty(TaobaoConstants.ITEM_TITLE))
			{
				shipping.item_title=json[TaobaoConstants.ITEM_TITLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_NAME))
			{
				shipping.receiver_name=json[TaobaoConstants.RECEIVER_NAME];
			}

			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_LOCATION))
			{
				shipping.receiver_location=json[TaobaoConstants.RECEIVER_LOCATION];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_MOBILE))
			{
				shipping.receiver_mobile=json[TaobaoConstants.RECEIVER_MOBILE];
			}
			if (json.hasOwnProperty(TaobaoConstants.RECEIVER_PHONE))
			{
				shipping.receiver_phone=json[TaobaoConstants.RECEIVER_PHONE];
			}

			if (json.hasOwnProperty(TaobaoConstants.STATUS))
			{
				shipping.status=json[TaobaoConstants.STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.TYPE))
			{
				shipping.type=json[TaobaoConstants.TYPE];
			}

			if (json.hasOwnProperty(TaobaoConstants.FREIGHT_PAYER))
			{
				shipping.freight_payer=json[TaobaoConstants.FREIGHT_PAYER];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_CONFIRM))
			{
				shipping.seller_confirm=json[TaobaoConstants.SELLER_CONFIRM];
			}
			if (json.hasOwnProperty(TaobaoConstants.COMPANY_NAME))
			{
				shipping.company_name=json[TaobaoConstants.COMPANY_NAME];
			}

			return shipping;
		}



		/**
		 * XML转换为Shipping
		 */
		private static function convertXMLToShipping(value:XML, shipping:Shipping):Shipping
		{
			return shipping;
		}

	}
}